package com.epfresh.basedialog;

import android.app.Activity;
import android.app.Dialog;
import android.content.Intent;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;

import com.epfresh.R;
import com.epfresh.api.global.AppApplication;
import com.epfresh.api.helper.ApplicationHelper;
import com.epfresh.api.utils.AppUtils;
import com.epfresh.api.utils.LocalDisplay;
import com.epfresh.api.utils.ToastUtils;
import com.umeng.socialize.Config;
import com.umeng.socialize.PlatformConfig;
import com.umeng.socialize.ShareAction;
import com.umeng.socialize.UMShareAPI;
import com.umeng.socialize.UMShareListener;
import com.umeng.socialize.bean.SHARE_MEDIA;
import com.umeng.socialize.media.UMImage;
import com.umeng.socialize.media.UMWeb;

public class ShareDialog extends Dialog implements View.OnClickListener {

    private View vCancel;
    private View vQQ;
    private View vQQZone;
    private View vWechat;
    private View vFriendly;
    private View vCopyUrl;
    private Activity activity;

    public ShareDialog(Activity context) {

        this(context,R.style.no_title_dialog);

    }

    public ShareDialog(Activity context, int theme) {
        super(context, theme);
        activity = context;
        builer();
    }

    public ShareDialog builer() {

        View v = LayoutInflater.from(getContext()).inflate(R.layout.layout_share, null);

        setContentView(v);
        Window dialogWindow = getWindow();
        dialogWindow.setGravity(Gravity.CENTER);
        WindowManager.LayoutParams lp = dialogWindow.getAttributes();
        lp.width = LocalDisplay.SCREEN_WIDTH_PIXELS -LocalDisplay.dp2px(30);
        dialogWindow.setAttributes(lp);

        vCancel = findViewById(R.id.v_cancel);
        vQQ = findViewById(R.id.rl_qq);
        vQQZone = findViewById(R.id.rl_qqzone);
        vWechat = findViewById(R.id.rl_wechat);
        vFriendly = findViewById(R.id.rl_weChat_circle);
        vCopyUrl = findViewById(R.id.tv_copy_share_url);
        vCancel.setOnClickListener(this);
        vQQ.setOnClickListener(this);
        vQQZone.setOnClickListener(this);
        vWechat.setOnClickListener(this);
        vFriendly.setOnClickListener(this);
        vCopyUrl.setOnClickListener(this);
        return this;
    }

    @Override
    public void show() {


    }

    public void showDialog() {
        super.show();
    }

    @Override
    public void onClick(View v) {
        dismiss();
        if (v.getId() == R.id.tv_copy_share_url) {
            if (pageUrl == null) {
                pageUrl = "";
            }
            shareUrl(pageUrl);
        } else {
            share(v.getId());
        }
//        switch (v.getId()) {
//            case R.id.v_cancel:
//
//                break;
//            case R.id.rl_qq:
//
//                break;
//            case R.id.rl_qqzone:
//
//                break;
//            case R.id.rl_wechat:
//
//                break;
//            case R.id.rl_wechat_friendly:
//
//                break;
//            case R.id.tv_copy_share_url:
//                shareUrl("e批生鲜");
//                break;
//            default:
//                break;
//        }
    }


    private String pageUrl;
    private String imageUrl;
    private String text;
    private String title;
    private String id;

    public void setShareContent(String url, String imageUrl, String text, String title, String id) {
        this.pageUrl = url;
        this.imageUrl = imageUrl;
        this.text = text;
        this.title = title;
        this.id = id;
    }

    public void showDialog(String url, String imageUrl, String text, String title, String id) {
        this.setShareContent(url, imageUrl, text, title, id);
        super.show();
    }

    public void showDialog(String url, String imageUrl, String text, String title) {
        this.setShareContent(url, imageUrl, text, title, null);
        super.show();
    }

    private void share(int type) {
        UMImage image = null;
        if (!TextUtils.isEmpty(imageUrl)) {
            image = new UMImage(activity, imageUrl);
        }
        String url = this.pageUrl;
        String text = this.text;
        String title = this.title;
//        Config.dialog = this;
        UMWeb  umWeb = new UMWeb(url,title,text,image);
        switch (type) {
            case R.id.rl_qq:
                new ShareAction(activity)
                        .setPlatform(SHARE_MEDIA.QQ).setCallback(umShareListener)
                        .withMedia(umWeb)
                        .share();
                break;
            case R.id.rl_qqzone:
                new ShareAction(activity)
                        .setPlatform(SHARE_MEDIA.QZONE).setCallback(umShareListener)
                        .withMedia(umWeb)
                        .share();
                break;
            case R.id.rl_wechat:
                new ShareAction(activity)
                        .setPlatform(SHARE_MEDIA.WEIXIN).setCallback(umShareListener)
                        .withMedia(umWeb)
                        .share();
                break;
            case R.id.rl_weChat_circle:
                new ShareAction(activity)
                        .setPlatform(SHARE_MEDIA.WEIXIN_CIRCLE).setCallback(umShareListener)
                        .withMedia(umWeb)
                        .share();
                break;
            default:
                break;
        }

    }

    private void shareUrl(String content) {
        AppUtils.copy(content, getContext());
        if (activity != null)
            ToastUtils.TextToast(getContext().getApplicationContext(), getContext().getString(R.string.share_url_copy_success));
    }


    private UMShareListener umShareListener = new UMShareListener() {

        @Override
        public void onStart(SHARE_MEDIA share_media) {

        }

        @Override
        public void onResult(SHARE_MEDIA platform) {

            if (activity != null)
                ToastUtils.TextToast(getContext().getApplicationContext(), ApplicationHelper.getInstance().getDoc().getShared());
        }

        @Override
        public void onError(SHARE_MEDIA platform, Throwable t) {
            if (activity != null)
                ToastUtils.TextToast(getContext().getApplicationContext(), "分享失败");
        }

        @Override
        public void onCancel(SHARE_MEDIA platform) {
//            if (activity != null)
//                Toast.makeText(getContext().getApplicationContext(), " 分享取消", Toast.LENGTH_SHORT).show();
        }
    };


    public void onResult(int requestCode, int resultCode, Intent data) {
        if (activity != null)
            UMShareAPI.get(activity).onActivityResult(requestCode, resultCode, data);
    }


}
